﻿using Dapper;
using Domain.Entity.zct;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class PermissionRepository : IPermissionRepository
    {
        
        //连接字符串
        string con = "Data Source=121.37.93.71;Initial Catalog=CSSL_ZCTWMS;User ID=sa;Password=geili@2025;Encrypt=False";


        //根据用户角色显示菜单
        public async Task<List<EPermission>> GetMeau(int rid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                string sql = $@"select P.permission_id,permission_code,permission_name,permission_address,permission_parentid from [dbo].[zct_permission] P
                                inner join [dbo].[zct_role_permission] RP on P.permission_id=RP.permission_id
                                where  RP.role_id={rid} and P.permission_status=0 and P.is_delete=0";
                List<Permission> permissions =(await sqlConnection.QueryAsync<Permission>(sql)).ToList();//根据角色id查询到的对应权限数据
                List<EPermission> ePermissions = createMenu(permissions, 0);
                return ePermissions;
            }
        }

        //把二维结构转换成层次结构 (数据，父id)
        private List<EPermission> createMenu(List<Permission> permissions, int pid)
        {
            //获取父级数据
            List<Permission> fu = permissions.Where(e => e.Permission_parentid == pid).ToList();

            List<EPermission> ePermissions = new List<EPermission>();
            foreach (Permission item in fu)
            {
                //Permission转化为EPermission
                EPermission ePermission = new EPermission()
                {
                    Permission_id=item.Permission_id,
                    Permission_code=item.Permission_code,
                    Permission_name=item.Permission_name,
                    Permission_address=item.Permission_address,
                    Permission_status=item.Permission_status,
                    Permission_parentid=item.Permission_parentid,
                    Permission_sort=item.Permission_sort,
                    Is_delete=item.Is_delete,
                    Remark=item.Remark,
                    Children=createMenu(permissions, item.Permission_id)
                };
                ePermissions.Add(ePermission);
            }
            return ePermissions;
        }

        // 获取所有权限并构建树形结构
        public async Task<List<EPermission>> GetAllPermissionTree()
        {
            // 获取所有权限数据
            var permissions = await GetAllPermissions();

            // 构建树形结构
            return BuildPermissionTree(permissions, 0);
        }

        // 从数据库获取所有权限
        public async Task<List<Permission>> GetAllPermissions()
        {
            using (SqlConnection connection = new SqlConnection(con))
            {
                await connection.OpenAsync();
                string sql = @"
            SELECT 
                permission_id AS Permission_id,
                permission_code AS Permission_code,
                permission_name AS Permission_name,
                permission_address AS Permission_address,
                permission_status AS Permission_status,
                permission_parentid AS Permission_parentid,
                permission_sort AS Permission_sort,
                is_delete AS Is_delete,
                remark AS Remark
            FROM zct_permission 
            WHERE is_delete = 0
            ORDER BY permission_sort";

                return (await connection.QueryAsync<Permission>(sql)).ToList();
            }
        }

        // 构建权限树
        private List<EPermission> BuildPermissionTree(List<Permission> permissions, int parentId)
        {
            return permissions
                .Where(p => p.Permission_parentid == parentId)
                .OrderBy(p => p.Permission_sort)
                .Select(item => new EPermission
                {
                    Permission_id = item.Permission_id,
                    Permission_code = item.Permission_code,
                    Permission_name = item.Permission_name,
                    Permission_address = item.Permission_address,
                    Permission_status = item.Permission_status,
                    Permission_parentid = item.Permission_parentid,
                    Permission_sort = item.Permission_sort,
                    Is_delete = item.Is_delete,
                    Remark = item.Remark,
                    Children = BuildPermissionTree(permissions, item.Permission_id)
                })
                .ToList();
        }
    }
}
